package com.hmis.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmis.domain.Bed;
import com.hmis.service.BedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@Controller
public class BedDAO implements InitializingBean {

    public static BedService bedService;
    public static BedDAO bedDAO;
    @Autowired
    private BedService bed;

    public List<Bed> listBed() {
        return bedService.list();
    }

    public Bed findByWardNoAndTheBedNo(Long wardNo, Long theBedNo) {
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bed::getWardNo, wardNo);
        queryWrapper.eq(Bed::getTheBedNo, theBedNo);
        return bedService.getOne(queryWrapper);
    }

    public void refreshBed(Long wardNo, Long theBedNo) {
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bed::getWardNo, wardNo);
        queryWrapper.eq(Bed::getTheBedNo, theBedNo);
        Bed bed = bedService.getOne(queryWrapper);
        bed.setStatus(0);
        bedService.updateById(bed);
    }

    public List<Bed> listBedsWithWardNo(Long wardNo) {
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bed::getWardNo,wardNo);
        return bedService.list(queryWrapper);
    }

    public void updateByEntity(Bed bed) {
        bedService.updateById(bed);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        bedService = bed;
        bedDAO = this;
    }

}
